package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.entity.Item;
import com.smartfactory.sail.entity.OrderItem;
import com.smartfactory.sail.entity.StorageRecord;
import com.smartfactory.sail.entity.enums.StorageStatusEnum;
import com.smartfactory.sail.mapper.OrderItemMapper;
import com.smartfactory.sail.mapper.StorageRecordMapper;
import com.smartfactory.sail.service.ICustService;
import com.smartfactory.sail.service.IItemService;
import com.smartfactory.sail.service.IPurchaseRecordService;
import com.smartfactory.sail.service.IStorageRecordService;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.item.ListMaterialStorageResp;
import com.smartfactory.sail.vo.item.ListProductsStorageResp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 仓库流水 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-07
 */
@Service
public class StorageRecordServiceImpl extends ServiceImpl<StorageRecordMapper, StorageRecord> implements IStorageRecordService {

    private StorageRecordMapper storageRecordMapper;
    private IItemService itemService;
    private ICustService iCustService;
    private IPurchaseRecordService iPurchaseRecordService;
    private OrderItemMapper orderItemMapper;

    public StorageRecordServiceImpl(StorageRecordMapper storageRecordMapper, IItemService itemService, ICustService iCustService, IPurchaseRecordService iPurchaseRecordService, OrderItemMapper orderItemMapper) {
        this.storageRecordMapper = storageRecordMapper;
        this.itemService = itemService;
        this.iCustService = iCustService;
        this.iPurchaseRecordService = iPurchaseRecordService;
        this.orderItemMapper = orderItemMapper;
    }

    @Override
    public Long waitIn(StorageRecord storageRecord) {
        return null;
    }

    /**
     * 原料库列表
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<ListMaterialStorageResp> listMateriaStorage(PageParam pageParam) {
        //分页信息
        Page<StorageRecord> page = new Page<>();
        page.setSize(pageParam.getSize());
        page.setCurrent(pageParam.getCurrentPage());
        IPage<StorageRecord> storageRecords = storageRecordMapper.selectCurrentAndWaitInForMateria(page);
        List<StorageRecord> records = storageRecords.getRecords();
        IPage<ListMaterialStorageResp> result = new Page<>();
        ArrayList<ListMaterialStorageResp> resps = new ArrayList<>();
        for (StorageRecord storageRecord : records) {
            if (storageRecord != null) {
                ListMaterialStorageResp resp = new ListMaterialStorageResp();
                Item item = itemService.getById(storageRecord.getItemId());
                resp.setItemId(storageRecord.getItemId());
                resp.setCode(item.getCode());
                resp.setName(item.getName());
                resp.setProductDrawings(item.getProductDrawings());
                resp.setSize(storageRecord.getSize());
                resp.setStatus(storageRecord.getStatus());
                resp.setStorageRecodeId(storageRecord.getId());
                if (storageRecord.getPurchaseRecordId() != null) {
                    Long supplierId = iPurchaseRecordService.getById(storageRecord.getPurchaseRecordId()).getSupplierId();
                    if (supplierId != null) {
                        resp.setSupplierCode(iCustService.getById(supplierId).getCustCode());
                    }
                } else {
                    resp.setSupplierCode("");
                }
                resps.add(resp);
            }
        }
        result.setCurrent(storageRecords.getCurrent());
        result.setPages(storageRecords.getPages());
        result.setSize(storageRecords.getSize());
        result.setTotal(storageRecords.getTotal());
        result.setRecords(resps);
        return result;
    }

    @Override
    public IPage<ListProductsStorageResp> listProductStorage(PageParam pageParam) {

        //分页信息
        Page<StorageRecord> page = new Page<>();
        page.setSize(pageParam.getSize());
        page.setCurrent(pageParam.getCurrentPage());
        IPage<StorageRecord> storageRecords = storageRecordMapper.selectCurrentAndWaitInForProduct(page);
        List<StorageRecord> records = storageRecords.getRecords();
        IPage<ListProductsStorageResp> result = new Page<>();
        ArrayList<ListProductsStorageResp> resps = new ArrayList<>();
        for (StorageRecord storageRecord : records) {
            ListProductsStorageResp resp = new ListProductsStorageResp();
            Item item = itemService.getById(storageRecord.getItemId());

            BeanUtils.copyProperties(item, resp);//copy产品信息
            BeanUtils.copyProperties(storageRecord, resp);//仓库信息

            resp.setStorageRecodeId(storageRecord.getId());
            QueryWrapper<OrderItem> query = new QueryWrapper<>();
            if (storageRecord.getOrderId() != null) {
                query.eq("orderId", storageRecord.getOrderId());
            } else {
                query.eq("itemId", item.getId()).orderByDesc("updateTime");
            }
            List<OrderItem> orderItems = orderItemMapper.selectList(query);
            if (orderItems == null || orderItems.size() == 0) {
                resp.setCustName("暂无订单信息");
            } else {
                resp.setCustName(orderItems.get(0).getCustName());
            }
            resps.add(resp);
        }
        result.setCurrent(storageRecords.getCurrent());
        result.setPages(storageRecords.getPages());
        result.setSize(storageRecords.getSize());
        result.setTotal(storageRecords.getTotal());
        result.setRecords(resps);
        return result;
    }

    @Override
    public Boolean updateStatus(StorageStatusEnum status, Long recordId) {

        StorageRecord record = new StorageRecord();
        record.setId(recordId);
        record.setStatus(status);
        return updateById(record);
    }
}
